<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>登录</title>
		<link rel="stylesheet" type="text/css" href="css/index.css"/>
        <script src="js/jquery.min.js"></script>
		<style >
		#verify_image {
        text-align: center;
        margin: 9px auto;
        } 

		</style>
	</head>
	<body> <div class="wel" >欢迎登录Sunlightʊə的博客网站</div>
        <div class="bg">
           

            <div class="user">
                <div id="yonghu">用户名</div>
                <input id="username" type="text" name="username" placeholder="请输入用户名">
            </div>


            <div class="password" >
	        	<div id="yonghu" >密&nbsp;&nbsp;&nbsp;码</div>
	       	    <input id="password" type="password" name="password" placeholder="请输入密码"  />
	        </div>


            <div class="code" >
	        	<div id="yonghu" >验证码</div>
	       	    <input  id="code_input"  type="password"  placeholder="请输入验证码"/>
	        </div>


            <div class="tuxing" >
                <div id="verify_image">
                    <img id="codeimg" alt="更换验证码" height="45" width="320" onclick="refreshCode()">
                </div>
            </div>

            

        


        

            <div class="codes">
				<a style="font-size: 11px;" href="reg1.html">注册</a>
			</div>

            <div class="fg" >
                    <a style="font-size: 11px;" href="forgot.html">忘记密码？</a>
            </div>



            <div class="login">
                <input type="button" value="登录" onclick="mysub()" id="submit-button">
            </div>
        </div>

        <script>
                    var count=0; // 记录错误登陆次数
        // 加载验证码
        function initCode(){
            jQuery.ajax({
                url:"/user/getcode",
                type:"POST",
                data:{ },
                success:function(res){
                    if(res.code==200 && res.data.length>0){
                      jQuery("#codeimg").attr("src",res.data);
                      jQuery("#codeimg").show();
                    }
                }
            });  
        }
        
        initCode();

        function refreshCode(){
            initCode();
        }

        function mysub() {
            // 1. 数据校验
            // 1.1 获取标签
            var username = jQuery("#username");
            var password = jQuery("#password");
            var code_input = jQuery("#code_input");
            // 1.2 用户名判断
            if(username.val().trim() == "") {
                alert("请输入用户名!");
                username.focus();
                return false;
            }

            // 1.3 密码判断
            if(password.val().trim() == '') {
                alert("请输入密码!");
                password.focus();
                return false;
            }

            // 冻结提交
           // jQuery("#submit-button").attr("disabled", "disabled");

            // 3. 发送数据给后端
            jQuery.ajax({
                url:"/user/login",
                type:"post",
                data:{
                    'username':username.val().trim(),
                    'password':password.val().trim(),
                    'code_input':code_input.val().trim()
                },
                success: function(res) {
                    if(res.code == 200 && res.data == 1) {
                        alert("登陆成功");
                        location.href = "myblog_list.html";
                        return;
                    }else 
                        alert("登陆失败 : " + res.msg);
                        refreshCode();
                        jQuery("#submit-button").removeAttr("disabled");
                        return false;
                    
                }
            });

        }

        </script>

	</body>
</html>
